How to Unzip Files in Linux

By 

Updated on

6 min read

Unzip Files in Linux

ZIP is the most widely used archive file format that supports lossless data compression. A ZIP file is a data container containing one or more compressed files or directories, which can later be extracted to restore the original files.

ZIP also offers a range of advanced features. You can password-protect your ZIP archives to keep your data safe or split large files into smaller parts to make them more manageable.

This article explains how to use the unzip command to extract and manage ZIP archives in Linux.

Quick Reference

OptionDescription
unzip file.zipExtract to current directory
unzip -d /path file.zipExtract to a specific directory
unzip -q file.zipQuiet mode, suppress output
unzip -o file.zipOverwrite existing files without prompting
unzip -n file.zipNever overwrite existing files
unzip -P pass file.zipExtract a password-protected archive
unzip -j file.zipFlatten directory structure on extract
unzip -l file.zipList archive contents
unzip -t file.zipTest archive integrity

For a printable quick reference, see the ZIP/UNZIP cheatsheet .

Installing unzip

The unzip utility is not installed by default in most Linux distributions. However, you can easily install it using your distribution’s package manager.

Install unzip on Ubuntu and Debian

Terminal
sudo apt install unzip

Install unzip on Fedora, RHEL, and Derivatives

Terminal
sudo dnf install unzip

How to Unzip a ZIP File

In its simplest form, when used without any options, the unzip command extracts all files from the specified ZIP archive to the current directory.

For instance, let’s say you downloaded the WordPress installation ZIP file. To unzip this file to the current directory, you’d simply run the following command:

Terminal
unzip latest.zip

ZIP files do not store ownership information. The extracted files are owned by the user that runs the command.

You must have write permissions on the directory where you are extracting the ZIP archive.

Suppress the Output of the unzip Command

By default, unzip prints the names of all the files it’s extracting, and it will also give you a summary once the extraction process is complete.

To suppress the printing of messages, use the -q option:

Terminal
unzip -q filename.zip

Unzip a ZIP File to a Different Directory

If you want to extract the contents of a ZIP file to a directory other than the present working directory, you can use the -d option. This allows you to specify the path of the destination directory where you want to extract the files.

Terminal
unzip filename.zip -d /path/to/directory

For example, to unzip the WordPress archive latest.zip to the /var/www/ directory, you’d use the following command:

Terminal
sudo unzip latest.zip -d /var/www

In the command above, we are using sudo because the user we are logged in as typically does not have write permissions to the /var/www directory. When ZIP files are decompressed using sudo, the extracted files and directories are owned by the user root.

Unzip a Password Protected ZIP File

If you have a ZIP file that is protected with a password and you want to extract its contents, you can use the unzip command with the -P option. This option allows you to specify the password required to open the file. Once you enter the correct password, the contents of the ZIP file will be extracted to the current directory.

Terminal
unzip -P PasswOrd filename.zip

Typing a password on the command line is insecure and should be avoided. A more secure option is to run the command without using the -P option.

If the ZIP file is password protected and you don’t use -P, unzip will prompt you to enter the password:

Terminal
unzip filename.zip

Type the password and press Enter.

output
archive:  filename.zip
[filename.zip] file.txt password: 

unzip will use the same password for all encrypted files in the archive as long as it is correct.

Test a ZIP File Without Extracting

To test a ZIP archive for errors without extracting it, use the -t option:

Terminal
unzip -t filename.zip

Exclude Files when Unzipping a ZIP File

When you want to exclude specific files or directories from being extracted from an archive, use the -x option, followed by a list of the archive files you want to exclude from the extraction process, separated by spaces.

Terminal
unzip filename.zip -x file1-to-exclude file2-to-exclude

In the following example, we are extracting all files and directories from the ZIP archive except the .git directory:

Terminal
unzip filename.zip -x "*.git/*"

Overwrite Existing Files

Let’s imagine a scenario where you have previously extracted the contents of a ZIP file, and now you are attempting to execute the same command again. In other words, you are trying to unzip the same file again.

Terminal
unzip latest.zip

By default, unzip asks whether you want to overwrite only the current file, overwrite all files, skip extraction of the current file, skip extraction of all files, or rename the current file.

output
Archive:  latest.zip
replace wordpress/xmlrpc.php? [y]es, [n]o, [A]ll, [N]one, [r]ename:

If you want to overwrite existing files without being prompted, use the -o option:

Terminal
unzip -o filename.zip

Use this option with caution. If you previously made any changes to the files, the changes are lost. In automation, -o is convenient, but it can overwrite files silently.

Unzip a ZIP File Without Overwriting Existing Files

Suppose you have unzipped a ZIP file and modified some of its files. However, you mistakenly deleted a few files that you now need to recover from the ZIP archive. What you want to do now is to restore the deleted files from the ZIP archive while keeping the changes you have made.

In this scenario, use the -n option, which instructs unzip to skip the extraction of a file if it already exists in the target directory:

Terminal
unzip -n filename.zip

Unzip Multiple ZIP Files

You can use regular expressions to match multiple archives.

For instance, if you have multiple ZIP files in your current working directory , you can unzip all files using only one command:

Terminal
unzip '*.zip'

Note the single quotes around *.zip. If you forget to quote the argument, the shell will expand the wildcard character, and you will get an error.

List the Contents of a ZIP File

To list the contents of a ZIP file, use the -l option:

Terminal
unzip -l filename.zip

In the example below, we are listing all WordPress installation files:

Terminal
unzip -l latest.zip

The output will look like this:

output
Archive:  latest.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2023-08-02 22:39   wordpress/
     3065  2021-08-31 18:31   wordpress/xmlrpc.php
      364  2021-12-19 12:20   wordpress/wp-blog-header.php
     7415  2023-03-18 17:13   wordpress/readme.html
...
...
    21323  2023-03-09 01:15   wordpress/wp-admin/themes.php
     8353  2022-09-10 18:20   wordpress/wp-admin/options-reading.php
     4620  2021-10-24 00:12   wordpress/wp-trackback.php
     1889  2023-05-03 00:11   wordpress/wp-comments-post.php
---------                     -------
 27271400                     1648 files

To show a more detailed listing, use -v:

Terminal
unzip -v filename.zip

Flatten Directory Structure When Extracting

By default, unzip recreates the directory structure stored inside the archive. If you want all files extracted into a single flat directory without any subdirectories, use the -j (junk paths) option:

Terminal
unzip -j filename.zip

This is useful when you only need the files themselves and do not care about the original folder layout inside the archive.

Conclusion

unzip is a command-line tool that allows you to list, test, and extract compressed ZIP archives.

To create a ZIP archive on a Linux system, you can use the zip command .

Feel free to leave a comment if you have any questions.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page